﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class Admin_QuanLyHangHoa_DanhSachHangHoa : System.Web.UI.Page
{
    private DataAccess da = new DataAccess();
    public Entities en = new Entities();
    private string strMaNhom = string.Empty;
    private string strMaHang = string.Empty;
    private string strTen = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["AdminID"] == null)
        {
            Response.Write("<script language='javascript'>alert('Bạn phải đăng nhập / You must login');location.href='/../Admin/Login.aspx';</script>");
        }
        if (!IsPostBack)
        {
            DDL_Nhomhang.Items.Clear();
            FillDropDownList();
            this.GetHangList();
        }
    }

    private void FillDropDownList()
    {
        SqlCommand comm1 = new SqlCommand("GetAllNhomHang", da.SqlConn());
        comm1.CommandType = CommandType.StoredProcedure;
        SqlDataReader drd1;
        comm1.Connection.Open();
        drd1 = comm1.ExecuteReader();
        while (drd1.Read())
        {
            ListItem ListNhom = new ListItem();
            ListNhom.Text = "" + drd1["TenNhomHang"].ToString();
            ListNhom.Value = drd1["MaNhomHang"].ToString();
            DDL_Nhomhang.Items.Add(ListNhom);
        }
        drd1.Close();
        comm1.Connection.Close();
        comm1.Connection.Dispose();
    }
    protected void GetHangList()
    {
        DataSet dts = new DataSet();
        SqlDataAdapter adt = new SqlDataAdapter();
        SqlCommand comm = new SqlCommand("GetHang", da.SqlConn());
        comm.CommandType = CommandType.StoredProcedure;
        comm.Parameters.AddWithValue("@MaNhom", strMaNhom);
        comm.Parameters.AddWithValue("@MaHang", strMaHang);
        comm.Parameters.AddWithValue("@Ten", strTen);
        comm.Connection.Open();
        adt.SelectCommand = comm;
        adt.Fill(dts);
        this.GV_NVList.DataSource = dts;
        this.GV_NVList.DataBind();
        dts.Clear();
        adt.Dispose();
        comm.Connection.Close();
        comm.Connection.Dispose();
    }
    protected void HangHoa_AddNew_Click(object sender, EventArgs e)
    {
        Response.Redirect("ThemMoiHangHoa.aspx");
    }
    protected void HangHoa_Delete_Click(object sender, EventArgs e)
    {
        this.DeleteHang();
    }
    protected void DeleteHang()
    {
        foreach (string item in HttpContext.Current.Request.Form)
        {
            if (item.Substring(0, 9) == "chbDelete")
            {

                string strMa = item.Substring(9, item.Length - 9);                
                SqlCommand comm = new SqlCommand("deleteHang", da.SqlConn());
                comm.CommandType = CommandType.StoredProcedure;
                comm.Parameters.AddWithValue("@Ma", strMa);
                try
                {
                    comm.Connection.Open();
                    comm.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message.ToString());
                }
                finally
                {
                    comm.Connection.Close();
                    comm.Connection.Dispose();
                }
                this.GetHangList();
            }
        }
    }
    protected void NVMASearch_Click(object sender, EventArgs e)
    {
        if (DDL_Nhomhang.SelectedValue.ToString() != "")
        {
            this.strMaNhom = DDL_Nhomhang.SelectedValue.ToString();
        }
        this.strMaHang = this.txtMaHang.Text;
        this.strTen = this.txtTenHang.Text;
        this.GetHangList();
    }
    protected void GV_NVList_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GV_NVList.PageIndex = e.NewPageIndex;
        this.GetHangList();
    }
    protected void display_Click(object sender, EventArgs e)
    {
        this.GetHangList();
    }
}
